package com.saifan.wyy_ov.ui.user;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;


import com.google.gson.Gson;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.StartActivity;
import com.saifan.wyy_ov.common.NetActivity;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.RoomInfoBean;

import com.saifan.wyy_ov.ui.communitycenter.RoomSelectActivity;
import com.saifan.wyy_ov.ui.home.MainActivity;
import com.saifan.wyy_ov.ui.set.Project;
import com.saifan.wyy_ov.ui.set.ServerSetActivity;

import com.saifan.wyy_ov.utils.ActivitysManager;
import com.saifan.wyy_ov.utils.ToastUtil;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


import lomasky.ma.httpUtils.JsonUtil;
import lomasky.ma.httpUtils.ResponseBean;
import lomasky.ma.utils.SharedPrefUtil;


public class LoginActivity extends NetActivity {
    EditText userName;
    EditText password;


    private android.widget.CheckBox rememberpassword;
    private Project project;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        this.rememberpassword = (CheckBox) findViewById(R.id.remember_password);
        mToolbar.setVisibility(View.GONE);
        this.password = (EditText) findViewById(R.id.password);
        this.userName = (EditText) findViewById(R.id.userName);
        String user = SharedPrefUtil.getString(this, "UserName", "");
        userName.setText(user);
        ActivitysManager.getManager().removeAllActivityExceptOne(LoginActivity.class);

    }


    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    public void login(View view) {


        if (userName.getText().toString().trim().equals("")){
            Toast.makeText(this,"用户不能为空",Toast.LENGTH_LONG).show();
            return;
        }

        if (userName.getText().toString().trim().equals("")){
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("UserName", userName.getText().toString());
        params.put("Password", password.getText().toString());
        post(Api.Customer.LOGIN, params, "正在登录");


    }


    public void register(View view) {
        startActivity(new Intent(this, RegisterActivity.class));

    }

    public void forgot(View view) {
        startActivity(new Intent(this, ForgotActivity.class));

    }

    @Override
    public void err(String e) {

        if (e.equals("404")){
            ToastUtil.showToast(this, "请设置授权项目");
            startActivity(ServerSetActivity.class);
        }else {
            showToast(e);
        }

    }

    @Override
    public void ok(ResponseBean response) {
        if (response.api.equals(Api.Customer.LOGIN)) {
            if (response.code == 200) {

                SharedPrefUtil.putString(this, "roomInfos", response.content);
                if (rememberpassword.isChecked()) {
                    SharedPrefUtil.putString(this, "UserName", userName.getText().toString());
                    SharedPrefUtil.putString(this, "Password", password.getText().toString());
                }

                List<RoomInfoBean> roomInfos = JsonUtil.getList(response.content, RoomInfoBean.class);
                Intent intent;
                if (roomInfos.size() == 1) {
                    if (roomInfos.get(0).getWYKHDA_KHYHM() == null) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
//                                startActivity(new Intent(LoginActivity.this, LoginActivity.class));


                            }
                        });
                        builder.setTitle("提示");
                        builder.setMessage("您还没在管理处登记电话，请您先到管理处登记,或者使用其它账号");
                        builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
                            @Override
                            public boolean onKey(DialogInterface dialog, int keyCode,
                                                 KeyEvent event) {
                                switch (keyCode) {
                                    case KeyEvent.KEYCODE_BACK:


                                        break;
                                    default:
                                        break;
                                }
                                return true;
                            }
                        });
                        builder.setCancelable(false);
                        builder.create().show();
                        return;
                    }


                    intent = new Intent(this, MainActivity.class);
                    SharedPrefUtil.putString(this, "roomInfo", new Gson().toJson(roomInfos.get(0)));
                    startActivity(intent);
                    close();

                } else if (roomInfos.size() > 1) {
//                    String WYFJDA_FJMC = SharedPrefUtil.getString(LoginActivity.this, "WYFJDA_FJMC", "");
//                    if (TextUtils.isEmpty(WYFJDA_FJMC)) {

                    intent = new Intent(LoginActivity.this, RoomSelectActivity.class);
                    intent.putExtra("must",true);
                    startActivity(intent);
//                    } else {
//                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
//                        startActivity(intent);
//                        close();
//                    }


                }

            } else {
                showToast("登录不成功");
            }
        }
    }


    public void settings(View view) {
        startActivity(new Intent(this, ServerSetActivity.class));
        close();
    }

    public void close() {
        ActivitysManager.getManager().removeActivity(this);
    }


    private int mBackKeyPressedTimes = 0;

    @Override
    public void onBackPressed() {

        if (mBackKeyPressedTimes == 0) {
            Toast.makeText(this, "再按一次退出程序 ", Toast.LENGTH_SHORT).show();
            mBackKeyPressedTimes = 1;
            new Thread() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        mBackKeyPressedTimes = 0;
                    }
                }
            }.start();
            return;
        } else {
            ActivitysManager.getManager().exit();
        }
    }

}
